function varargout = ExploreData(varargin)
% EXPLOREDATA M-file for ExploreData.fig
%      EXPLOREDATA, by itself, creates a new EXPLOREDATA or raises the existing
%      singleton*.
%
%      H = EXPLOREDATA returns the handle to a new EXPLOREDATA or the handle to
%      the existing singleton*.
%
%      EXPLOREDATA('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in EXPLOREDATA.M with the given input arguments.
%
%      EXPLOREDATA('Property','Value',...) creates a new EXPLOREDATA or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ExploreData_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ExploreData_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ExploreData

% Last Modified by GUIDE v2.5 20-Feb-2014 13:22:48

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ExploreData_OpeningFcn, ...
                   'gui_OutputFcn',  @ExploreData_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before ExploreData is made visible.
function ExploreData_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ExploreData (see VARARGIN)

% Choose default command line output for ExploreData
handles.output = hObject;

% Set default root directory
handles.root_dir = 'D:\data';

% Update handles structure
guidata(hObject, handles);

% This sets up the initial plot - only do when we are invisible
% so windowerr.identifier can get raised using ExploreData.
% if strcmp(get(hObject,'Visible'),'off')
%     plot(rand(5));
% end

% UIWAIT makes ExploreData wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = ExploreData_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes1);
cla;

popup_sel_index_1 = get(handles.popupmenu1, 'Value');
popup_sel_index_2 = get(handles.popupmenu2, 'Value');

chan_name_1 = GetChanName(popup_sel_index_1);

% root_path = '/home/vanderbruggen/Dropbox/post_doc_icfo/data_mining/data_sample';
root_path = handles.root_dir;

% Start date
str = get(handles.popupmenu_start_year,'String');
date_start.year = str2num(str{get(handles.popupmenu_start_year,'Value')});
date_start.month = get(handles.popupmenu_start_month,'Value');
date_start.day = get(handles.popupmenu_start_day,'Value');

% End date
str = get(handles.popupmenu_end_year,'String');
date_end.year = str2num(str{get(handles.popupmenu_end_year,'Value')});
date_end.month = get(handles.popupmenu_end_month,'Value');
date_end.day = get(handles.popupmenu_end_day,'Value');

% Decimation
str = get(handles.popupmenu_decimate,'String');
n_decimate = str2num(str{get(handles.popupmenu_decimate,'Value')});

if popup_sel_index_2 == 1 % Plot vs date
    [date, monitor_data] = LoadMonitorData(root_path, date_start, date_end, chan_name_1, n_decimate);
    plot(date, monitor_data, '.')
    datetick('x','yyyy-mmm-dd')
else % Plot vs another channel
    chan_name_2 = GetChanName(popup_sel_index_2 - 1);
    [~, monitor_data] = LoadMonitorData(root_path, date_start, date_end, {chan_name_1,chan_name_2}, n_decimate);
    plot(monitor_data(:,2), monitor_data(:,1), '.')    
end

% --- Return channel name from popup selection index
function chan_name = GetChanName(popup_sel_index)
switch popup_sel_index
    case 1
        chan_name = 'CURR_ION_PUMP';
    case 2
        chan_name = 'LOCK_FIB_LAS_1560';
    case 3
        chan_name = 'MOT_BEAMS_POWER';
    case 4
        chan_name = 'LOCK_REPUMP';
    case 5
        chan_name = 'LOCK_COOLER';
    case 6
        chan_name = 'MOT_PD_FLUOR';
    case 7
        chan_name = 'MOT_MAG_FIELD';
    case 8
        chan_name = 'AC_LINE';
    case 9
        chan_name = 'INTEGRATOR_COOLING_LOCK';
    case 10
        chan_name = 'PD1';
    case 11
        chan_name = 'PD_MONITOR_780';
    case 12
        chan_name = 'PD_MONITOR_1529';
    case 13
        chan_name = 'DROP_SIGNAL';
    case 14
        chan_name = 'LOCK_FIB_LAS_1529';
    case 15
        chan_name = 'PD_SIGNAL_1';
    case 16
        chan_name = 'PIEZO_SIG_COOLER_LOCK';
    case 17
        chan_name = 'X_COIL_CURR_MONITOR';
    case 18
        chan_name = 'Y_COIL_CURR_MONITOR';
    case 19
        chan_name = 'Z_COIL_CURR_MONITOR';
    otherwise
        error('This should not happen !!')
end

% --------------------------------------------------------------------
function FileMenu_Callback(hObject, eventdata, handles)
% hObject    handle to FileMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to OpenMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.root_dir = uigetdir(handles.root_dir,'Select data root directory');

% --------------------------------------------------------------------
function PrintMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to PrintMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
printdlg(handles.figure1)

% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to CloseMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],...
                     ['Close ' get(handles.figure1,'Name') '...'],...
                     'Yes','No','Yes');
if strcmp(selection,'No')
    return;
end

delete(handles.figure1)


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
     set(hObject,'BackgroundColor','white');
end

% set(hObject, 'String', {'plot(rand(5))', 'plot(sin(1:0.01:25))', 'bar(1:.5:10)', 'plot(membrane)', 'surf(peaks)'});


% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2


% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu2.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2


% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu_start_year.
function popupmenu_start_year_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_start_year (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_start_year contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_start_year


% --- Executes during object creation, after setting all properties.
function popupmenu_start_year_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_start_year (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu_end_year.
function popupmenu_end_year_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_end_year (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_end_year contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_end_year


% --- Executes during object creation, after setting all properties.
function popupmenu_end_year_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_end_year (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu_start_month.
function popupmenu_start_month_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_start_month (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_start_month contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_start_month


% --- Executes during object creation, after setting all properties.
function popupmenu_start_month_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_start_month (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu_end_month.
function popupmenu_end_month_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_end_month (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_end_month contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_end_month


% --- Executes during object creation, after setting all properties.
function popupmenu_end_month_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_end_month (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu_start_day.
function popupmenu_start_day_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_start_day (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_start_day contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_start_day


% --- Executes during object creation, after setting all properties.
function popupmenu_start_day_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_start_day (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu_end_day.
function popupmenu_end_day_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_end_day (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_end_day contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_end_day


% --- Executes during object creation, after setting all properties.
function popupmenu_end_day_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_end_day (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_decimate_Callback(hObject, eventdata, handles)
% hObject    handle to edit_decimate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_decimate as text
%        str2double(get(hObject,'String')) returns contents of edit_decimate as a double


% --- Executes during object creation, after setting all properties.
function edit_decimate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_decimate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu_decimate.
function popupmenu_decimate_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_decimate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_decimate contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_decimate


% --- Executes during object creation, after setting all properties.
function popupmenu_decimate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_decimate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
